clearvars -except b
load tprob_data 



num_coef = 4;

M = max(tprob_m);
pxest = zeros(M,num_coef);sigrhoest=zeros(M,1);

for m = 1:M
    index = find(tprob_m==m);
    cons = ones(length(index),1);
    [temp ,~,r,~,stats] = regress(v_crime(index), [cons Lv_crime(index) tprob_t(index) Lpm10(index)]);
    pxest(m,:) = temp;
end
pxest = pxest';

T= 7; beta = 0.95;

gamma_tprob =  gammafunc(T,beta, M, pxest);

load est_res_s2 rentd  M con_m 
load datafromstata gamma

statmwtp = rentd;

picmwtpr1 = 0*rentd; 
for m = 1:M
    index = find(con_m==m);
    picmwtpr1(index) = statmwtp(index)/gamma_tprob(m,1);
end

for m = 1:M
    index = find(con_m==m);
    res2(m,:) = mean(picmwtpr1(index));
end

rob_tprob = res2;
save rob_tprob rob_tprob 
